package com.beauty.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * Created by frinder_liu on 2016/2/27.
 */
public class SpringUtil {

	/**
	 * applicationContext 对象
	 */
	public static final WebApplicationContext _CTX = ContextLoader.getCurrentWebApplicationContext();

	/**
	 * 获取bean
	 *
	 * @param name
	 * @param <T>
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static <T> T getBean(String name) {
		return (T) _CTX.getBean(name);
	}

	/**
	 * 获取request
	 *
	 * @return
	 */
	public static HttpServletRequest getRequest() {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		return request;
	}

	/**
	 * 获取session
	 *
	 * @return
	 */
	public static HttpSession getSession() {
		return getRequest().getSession();
	}

}
